/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.exception;

import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * This class was developed originally as a 
 * NestedRuntimeException. I am sure there is
 * code out on google to replace this code
 * but in the mean time we will borrow it. 
 */
public class DatabaseFacadeException extends Exception {
	  Throwable nestedEx=null;
	  
	  public DatabaseFacadeException(String message){
	    super(message);
	  }
	  public DatabaseFacadeException(Throwable e){
	    this(e, "");
	  }
	  public DatabaseFacadeException(Throwable e, String message){
	    this(message);
	    nestedEx=e;
	  }
	  public Throwable getNestedException(){
	    return nestedEx;
	  }
	  protected void doActualStackTrace(PrintWriter s){
	    super.printStackTrace(s);
	  }
	  
	  public void printStackTrace(PrintStream s){    
	    printStackTrace(new PrintWriter(new OutputStreamWriter(s)));
	  }
	  public void printStackTrace(PrintWriter s){
	    doStaticStackTrace(s, this, nestedEx); 
	  }
	  public void doStaticStackTrace(PrintWriter s, Throwable mainX, Throwable nestX){

	    try {    
	      boolean thisHasMessage=hasMessage(mainX);
	      if (thisHasMessage || nestX==null){
	        if (mainX instanceof DatabaseFacadeException)
	          ((DatabaseFacadeException)mainX).doActualStackTrace(s);
	        else
	        if (mainX instanceof DatabaseFacadeException)
	          ((DatabaseFacadeException)mainX).doActualStackTrace(s);
	        else {
	          Throwable e=mainX.getCause();
	          if (e==null || mainX.getMessage().indexOf(e.toString())==-1 )
	            mainX.printStackTrace(s);
	        }
	      }
	      
	      if (nestX==null){
	        return;
	      }
	 
	      if (thisHasMessage)
	        s.write("\n");
	  
	      if (nestX instanceof DatabaseFacadeException){
	    	  nestX.printStackTrace(s);
	      }
	      else {

	        doStaticStackTrace(s, nestX, nestX.getCause());
	      }
	    } finally {
	      s.flush();
	    }
	  }
	  private boolean hasMessage(Throwable t){
	    if (t==null)
	      return false;
	    else
	    if (t.getMessage()==null || t.getMessage().equals(""))
	      return false;
	    else
	      return true;
	  } 
}
